'use strict';
const Service = require('egg').Service;
const md5 = require('js-md5')
const {
  ERROR,
  SUCCESS,
} = require('../util/util');
class UserService extends Service {
    //用户注册
  async create(user) {
    const {
      ctx,
    } = this;
    try {
      if (!user.username || !user.password) {
        ctx.status = 400;
        return Object.assign(ERROR, {
          msg: '请输入用户名和密码'
        });
      }
      const md5Passwd = md5(user.password)
      user = Object.assign(user, {
        password: md5Passwd,
      });
      const result = await ctx.model.User.findOne({
        where: {
          username: user.username,
        },
      });
      if (!result) {
        const res = await this.ctx.model.User.create(user);
        ctx.status = 201;
        return Object.assign(SUCCESS, {
          data: res,
        });
      }
      ctx.status = 406;
      return Object.assign(ERROR, {
        msg: '用户名已经存在',
      });

    } catch (error) {
      ctx.status = 500;
      throw (error);
    }
  }
 
  //删除用户
  async del(id) {
    const {
      ctx,
    } = this;
    try {
         //1.查找用户id
      const user = await ctx.model.User.findById(id);
          //2.进行判断如果id不存在就代表用户不存在，删除不了，提示报错，'该用户不存在'
      if (!user) {
        ctx.status = 400;
        return Object.assign(ERROR, {
          msg: '用户不存在',
        });
      }
      //3.用户存在，删除用户
      user.destroy();
      ctx.status = 200;
      return Object.assign(SUCCESS, {
        data: user,
      });

    } catch (error) {
      ctx.throw(500);
    }
  }
  //修改用户
  async update({ id, user }) {
    const {
      ctx,
    } = this;
    try {
      //1.通过id  查找得到一条数据
      const userDB = await ctx.model.User.findById(id);
      //2.如果数据不存在就表示用户不存在
      if (!userDB) {
        ctx.status = 400;
        return Object.assign(ERROR, {
          msg: '找不到该用户',
        });
      }
      const md5Passwd = md5(user.password)
      user = Object.assign(user, {
        password: md5Passwd,
      });
      //3.用户存在，我们将查找到的数据进行数据库操作更新(数据库更新)
      const res = await userDB.update(user);
      ctx.status = 200;
      //4.更新得到的解果赋值给user
      return Object.assign(SUCCESS, {
        data: res,
      });

    } catch (error) {
      ctx.throw(500);
    }
  }


   //用户登陆
  async login({ username, password }) {
    const {
      ctx,
    } = this;
    try {
      const user = await ctx.model.User.findOne({
        // 可以传递一个 where 对象来过滤查询,查询条件为 用户名
        where: {
          username: username
        },
      });
      if (!user) {
        return Object.assign(ERROR, {
          msg: 'username is error',
        });
      }
      if (md5(password) === user.password) {
        ctx.status = 200;
        const hash = md5.hex(password)
        ctx.cookies.set('token', hash, {
          httpOnly: false,
          signed: false,
          maxAge: 3600 * 1000,
          path: '/',
        });
        ctx.cookies.set('user_id', user.id, {
          httpOnly: false,
          signed: false,
          maxAge: 3600 * 1000,
          path: '/',
        });
        return Object.assign(SUCCESS, {
          data: Object.assign(user, {
            password: '',
          }),
        });
      }
      return Object.assign(ERROR, {
        msg: 'password is error',
      });


    } catch (error) {
      ctx.status = 500;
      throw (error);
    }
  }

  async find(id) {
    const {
      ctx,
    } = this;
    try {
      const user = await ctx.model.User.findById(id, {
        include: [{
          // model: ctx.model.Authority,
          attributes: [ 'id', 'name' ],
        }],
      });
      if (!user) {
        ctx.status = 401;
        return Object.assign(ERROR, {
          msg: 'user not found',
        });
      }
      ctx.status = 200;
      return Object.assign(SUCCESS, {
        data: user,
      });

    } catch (error) {
      throw (500);
    }
  }
}

module.exports = UserService;
